package com.jh.paymentchannel.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.jh.paymentchannel.util.Util;
import com.jh.paymentchannel.util.ump.exception.VerifyException;
import com.jh.paymentchannel.util.ump.paygate.v40.HttpRequest;
import com.jh.paymentchannel.util.ump.paygate.v40.Mer2Plat_v40;
import com.jh.paymentchannel.util.ump.paygate.v40.Plat2Mer_v40;
import com.jnewsdk.util.SignUtil;

import cn.jh.common.utils.Md5Util;
import net.sf.json.JSONObject;


@Controller
@EnableAutoConfiguration
public class SFinfoBackService {

	private static final Logger log = LoggerFactory.getLogger(SFinfoBackService.class);
	@Autowired
	Util util;
	
	@Value("${payment.ipAddress}")
	private String ipAddress;
	
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/sf/infoback")
	public @ResponseBody Object topupRequest(HttpServletRequest request,
			@RequestParam(value = "out_trade_no") String out_trade_no,
			@RequestParam(value = "total_fee") String total_fee,
			 @RequestParam(value = "body") String body,
			 @RequestParam(value = "mch_id") String mch_id,
				@RequestParam(value = "acc_bank_name") String acc_bank_name,
				 @RequestParam(value = "acct_type") String acct_type,
				 @RequestParam(value = "acc_name") String acc_name,
					@RequestParam(value = "acc_no") String acc_no,
					 @RequestParam(value = "mobile") String mobile,
					 @RequestParam(value = "id_no") String id_no,
						@RequestParam(value = "bank_code") String bank_code,
						 @RequestParam(value = "expire_date") String expire_date,
						 @RequestParam(value = "cvv") String cvv,
							 @RequestParam(value = "sign") String sign
			 ) throws IOException{
		log.info("into pageback  ================ ");
		Map<String, Object> map = new TreeMap<String, Object>(
		         new Comparator<String>() {
		             @Override
					public int compare(String obj1, String obj2) {
		             return obj1.compareTo(obj2);
		           }
		        });
				map.put("sp_id", "1102-2");  
				map.put("mch_id", mch_id);  
				map.put("out_trade_no", out_trade_no);
				map.put("total_fee", total_fee);
				map.put("body", body);
				map.put("notify_url", ipAddress+"/v1.0/paymentchannel/topup/sf/notify_call");  
				map.put("id_type", "01");  
				map.put("acc_bank_name", acc_bank_name);
				map.put("acct_type", acct_type);
				map.put("acc_name", acc_name);
				map.put("acc_no", acc_no);  
				map.put("mobile", mobile);  
				map.put("id_no", id_no);
				map.put("bank_code", bank_code);
				map.put("expire_date", expire_date);
				map.put("cvv", cvv);
				map.put("nonce_str", out_trade_no);
				map.put("sign", sign);
			
		    log.info("---------------------——map@"+map.toString());
		    String signMsg = SignUtil.getSignMsg(map, null);
			String url = "http://api.shangfudata.com/gate/epay/senapply";
			log.info("--------------------——signMsg@"+signMsg);
			String msg = HttpRequest.sendPost(url, signMsg);
			log.info("---------------------——msg@"+msg.toString());
			if(!msg.contains("{")) {
				msg = "{"+msg+"}";
			}
			Map mapp = new HashMap();
			if(msg.isEmpty()||null==msg) {
				mapp.put("message", "响应数据为空");
				mapp.put("code", "failed");	
				mapp.put("channel_type", "sf_pay");
				return map;
			}
			JSONObject json = JSONObject.fromObject(msg); 
			log.info("--------------------——json@"+json);
			if("SUCCESS".equals(json.getString("status"))) {
				if("SUCCESS".equals(json.getString("result_code"))) {
					mapp.put("code", "success");
				}else {
					if(json.containsKey("err_msg")) {
						mapp.put("message", json.getString("err_msg"));
					}else {
						mapp.put("message", "失败原因不明");
					}
					mapp.put("code", "failed");	
					mapp.put("channel_type", "sf_pay");
				}
			}else {
				mapp.put("message", json.getString("message"));
				mapp.put("code", "failed");	
				mapp.put("channel_type", "sf_pay");
			}
	       return mapp;
	}
		
	
}
